/** ***********************************************************************************
* @file         mon_task.c
* @brief        系统监控管理任务
* @details      主要是用来监控系统任务的状态、系统的内存状态、系统运行状态等操作；
* @author       杨小珛
* @date         2020/12/27
* @copyright    北京高知科技有限公司    
* @version      V1.0
* @attention    无
**************************************************************************************/

#include "mon_task.h"
#include "log_task.h"
#include "osi_mem.h"
#include "event_task.h"
#include "port_task.h"
#include "rtc_task.h"
#include "gnss_task.h"
#include "flash_dev.h"
#include "mcu_data_format.h"
#include "gaoz_api.h"
/*-------------------------------------------------------------------------
        全局变量定义(Global variable definition)
-----------------------------------------------------------------------*/
#define MAX_TASK_NUM   ( 50 )
#define MON_TASK_NAME  "MON_TASK"

GaozSemaphoreHandle_t sys_wakeup_semaphore;
GaozSemaphoreHandle_t mon_system_semaphore;
GaozMutexHandle_t mon_system_mutex;     /*系统监控任务互斥体*/
volatile uint32_t task_dog_count;       /*任务看门狗数量*/
volatile uint32_t mon_tick_counter;    
volatile uint32_t sys_auto_reset;       /*系统自动复位标识*/
volatile uint32_t sys_wakeup_sem_reg;  
system_abnormal_reset_e system_abnormal_reset;
system_block_mark_e system_block_mark;

system_wakeup_timer_t system_wakeup[SYSTEM_WAKEUP_TIMER_NUM];
task_dog_typedef task_dog_t[MAX_TASK_NUM];
system_run_state_s sys_run_s;
uint32_t system_timer_handle;
uint32_t system_timer_counter_reg;

/*-------------------------------------------------------------------------
        内部函数定义(Internal function definition)
-----------------------------------------------------------------------*/
static void mon_system_task( void * pvParameters);
static uint8_t system_check_run_time( void );
static void system_task_dog_manage(void);
static void system_run_time_detection(void);
static void system_free_space_monitoring(void);
static void system_check_spaces_num(void);
static void system_led_gpio_init( void );

static void system_net_led_state_control( system_led_mode_s mode );
static void system_gnss_led_state_control( system_led_mode_s mode );
static void system_check_run_status(void);

static void system_wakeup_queue_create(void);
static uint32_t system_wakeup_queue_get( uint32_t *msg );

static void system_into_sleep_work(void);
static uint32_t mon_wakeup_task_dog( void );

static void system_software_timer_function(void *arg);

static void system_software_timer_start( void );
static void system_software_timer_stop( void );

static uint32_t system_wakeup_signal_detection( uint32_t msg );
static void system_into_reset_work(void);
static void system_timer_anomaly_detection(void);

/** ****************************************************************************
* @remarks       void mon_create_task( unsigned portBASE_TYPE uxPriority )
* @brief         创建系统监听任务以及初始化数据
* @param[in]     uxPriority 优先级
* @param[out]    无
* @return        无
* @attention     在系统启动后进行创建
*******************************************************************************/
void mon_create_task( unsigned portBASE_TYPE uxPriority )
{   
    task_dog_count = 0;
    mon_tick_counter = 0;
    sys_wakeup_sem_reg = 0;
    system_timer_handle   = 0;
    system_timer_counter_reg = 0;
    system_abnormal_reset = SYS_RESET_INIT;
    system_block_mark = SYS_RESUME;
    mon_system_mutex = gaoz_mutex_create();     
    if(mon_system_mutex == 0) 
    {
        while(1);/*创建失败*/
    }
    system_wakeup_queue_create();
    system_led_gpio_init();
    system_software_timer_start();
    memset((uint8_t *)&sys_run_s ,0x00 ,sizeof(system_run_state_s));
    gaoz_watchdog_enable( 5 ); /*启动系统看门狗，设置的时间为5S钟*/
    sys_work_mode_set( SYS_ST_START );
    memset((uint8_t *)task_dog_t,0x00,MAX_TASK_NUM*sizeof(task_dog_typedef));
    memset((uint8_t *)system_wakeup,0x00,SYSTEM_WAKEUP_TIMER_NUM*sizeof(system_wakeup_timer_t));
    gaoz_thread_create(mon_system_task, MON_TASK_NAME, 1024*2, NULL, uxPriority);
}

/** ****************************************************************************
* @remarks       static void system_led_gpio_init( void )
* @brief         系统状态LED灯控制引脚初始化
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_led_gpio_init( void )
{
    gaoz_lpg_switch( 0 );
    gaoz_gpio_pull_disable(SYS_NET_LED_POWER_PIN);
    gaoz_gpio_mode_set(SYS_NET_LED_POWER_PIN,2);
    gaoz_gpio_cfg(SYS_NET_LED_POWER_PIN,1);
    gaoz_gpio_set(SYS_NET_LED_POWER_PIN,0);
    
    gaoz_gpio_pull_disable(SYS_GNSS_LED_POWER_PIN);
    gaoz_gpio_mode_set(SYS_GNSS_LED_POWER_PIN,0);
    gaoz_gpio_cfg(SYS_GNSS_LED_POWER_PIN,1);
    gaoz_gpio_set(SYS_GNSS_LED_POWER_PIN,0);
}

/** ****************************************************************************
* @remarks       static void system_net_led_state_control( void )
* @brief         网络LED状态指示控制
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_net_led_state_control( system_led_mode_s mode )
{
    #define LED_ON ( 0x01 )
    #define LED_OFF ( 0x00 )
    static uint8_t led_st =LED_OFF;
    static uint8_t led_time = 0; 
    static uint8_t led_on_time = 0; 
    static uint8_t led_off_time = 0; 
    
    if( mode == LED_OPEN )
    {
        led_on_time = 0;   
        led_off_time = 0;  
        led_st = LED_ON;
    }
    else if( mode == LED_ABNORMAL ) 
    {
        led_on_time = 1;    /* LED ON 100ms */
        led_off_time = 1;   /* LED OFF 100ms */
    }
    else if( mode == LED_NORMAL )  /*系统复位*/
    {
        led_on_time = 1;    /* LED ON 100ms */
        led_off_time = 9;   /* LED OFF 900ms */
    }
    else if( mode == LED_CLOSE )
    {
        led_on_time = 0;
        led_off_time = 0;  
        led_st = LED_OFF;
    }
    else if( mode == LED_WANKEUP )
    {
        led_on_time = 5;    /* LED ON 500ms */
        led_off_time = 5;   /* LED OFF 500ms */
    }
    /*控制系统LED灯状态*/
    if(( led_on_time != 0 ) && ( led_off_time != 0 ))
    {
        if( led_st == LED_ON )
        {   
            if(( ++led_time ) >= led_on_time )
            {
                led_time = 0;
                led_st = LED_OFF;
            }
        }
        else if( led_st == LED_OFF )
        {
            if(( ++led_time ) >= led_off_time ) /*系统监控任务休息周期为100ms*1 = 100ms*/
            {
                led_time = 0;
                led_st = LED_ON;
            }
        }
    }
    gaoz_gpio_set(SYS_NET_LED_POWER_PIN,led_st); 
}

/** ****************************************************************************
* @remarks       static void system_gnss_led_state_control( void )
* @brief         GNSS LED状态指示控制
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_gnss_led_state_control( system_led_mode_s mode )
{
    #define LED_ON ( 0x01 )
    #define LED_OFF ( 0x00 )
    static uint8_t led_st =LED_OFF;
    static uint8_t led_time = 0; 
    static uint8_t led_on_time = 0; 
    static uint8_t led_off_time = 0; 
    
    if( mode == LED_OPEN )
    {
        led_on_time = 0;   
        led_off_time = 0;  
        led_st = LED_ON;
    }
    else if( mode == LED_ABNORMAL ) 
    {
        led_on_time = 1;    /* LED ON 100ms */
        led_off_time = 1;   /* LED OFF 100ms */
    }
    else if( mode == LED_NORMAL )
    {
        led_on_time = 1;    /* LED ON 100ms */
        led_off_time = 9;   /* LED OFF 900ms */
    }
    else if( mode == LED_CLOSE )
    {
        led_on_time = 0;
        led_off_time = 0;
        led_st = LED_OFF;
    }
    /*控制系统LED灯状态*/
    if(( led_on_time != 0 ) && ( led_off_time != 0 ))
    {
        if( led_st == LED_ON )
        {   
            if(( ++led_time ) >= led_on_time )
            {
                led_time = 0;
                led_st = LED_OFF;
            }
        }
        else if( led_st == LED_OFF )
        {
            if(( ++led_time ) >= led_off_time ) /*系统监控任务休息周期为100ms*1 = 100ms*/
            {
                led_time = 0;
                led_st = LED_ON;
            }
        }
    }
    gaoz_gpio_set(SYS_GNSS_LED_POWER_PIN,led_st); 
}

/** ****************************************************************************
* @remarks       static void system_software_timer_function(void *arg)
* @brief         RTC中断处理函数
* @param[in]     *arg 输入参数
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_software_timer_function(void *arg)
{       
    system_timer_counter_reg++;
}

/** ****************************************************************************
* @remarks       void system_software_timer_start( void )
* @brief         启动定时器，周期性循环1秒钟
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_software_timer_start( void )
{
    #define SYSTEM_SOFTWARE_TIMER   ( 1000 )
    /*创建定时器功能，定时时间1S钟*/
    system_timer_handle = gaoz_timer_period_new( SYSTEM_SOFTWARE_TIMER, system_software_timer_function, NULL );
    if( system_timer_handle == 0 )
    {
        while(1);
    }
}

/** ****************************************************************************
* @remarks       void system_software_timer_stop( void )
* @brief         停止定时器
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_software_timer_stop( void )
{
    if( system_timer_handle != 0 )
    {
        if( gaoz_timer_free( system_timer_handle ) == 0 )
        {
            system_timer_handle = 0;
        }
    }
}

/** ****************************************************************************
* @remarks       static void system_wakeup_queue_create(void)
* @brief         创建系统唤醒消息队列
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_wakeup_queue_create(void)
{
    sys_wakeup_semaphore = gaoz_sem_new(0);
    if( sys_wakeup_semaphore == 0 )
    {
        while( 1 ); /*创建失败,进行处理*/
    }
}

/** ****************************************************************************
* @remarks       static uint32_t system_wakeup_queue_get( uint32_t *msg )
* @brief         获取系统唤醒消息队列信息
* @param[in]     *msg 获取的数据地址
* @param[out]    无
* @return        pdPASS 接收到数据 pdFAIL 未接收到数据
* @attention     必须在任务中运行
*******************************************************************************/
static uint32_t system_wakeup_queue_get( uint32_t *msg )
{
    uint32_t res = pdFAIL;
    
    if( sys_wakeup_semaphore == 0 )
    {
        system_wakeup_queue_create();
    }
    if( gaoz_sem_try_wait( sys_wakeup_semaphore , 1000 ) == pdPASS )
    {
        *msg = sys_wakeup_sem_reg;
        res = pdPASS;
    }
    return res;
}

/** ****************************************************************************
* @remarks       static uint32_t system_wakeup_signal_detection( uint32_t *msg )
* @brief         系统唤醒信号检测
* @param[in]     *msg 唤醒方式
* @param[out]    无
* @return        pdPASS 成功  pdFAIL 失败
* @attention     必须在任务中运行
*******************************************************************************/
static uint32_t system_wakeup_signal_detection( uint32_t msg )
{
    uint32_t res = pdFAIL;
    switch( msg )
    {
        case SYS_WAKEUP_TRIG_RTC:   /* 1 RTC中断触发唤醒 */
            //if(sys_switch_state_get( SYS_WRTC ) == SYS_WRTC_INIT )
            //{
                res = pdPASS;
            //}
        break;
        case SYS_WAKEUP_TRIG_BLE:   /* 3 蓝牙接口事件唤醒 */
            if((sys_switch_state_get( SYS_WBLE ) == SYS_WBLE_INIT ) || (sys_switch_state_get( SYS_WBLE ) == SYS_WBLE_SLEEPING ))
            {
                res = pdPASS;
            }
        break;
        case SYS_WAKEUP_TRIG_CAN:   /* 4 CAN总线事件唤醒*/
            if((sys_switch_state_get( SYS_WCAN ) == SYS_WCAN_INIT ) || ( sys_switch_state_get( SYS_WCAN ) == SYS_WCAN_SLEEPING ))
            {
                res = pdPASS;
            }
        break;
        case SYS_WAKEUP_TRIG_GSEN:  /* 5 GSEN中断事件唤醒 */
            if((sys_switch_state_get( SYS_WGSEN ) == SYS_WGSEN_INIT ) || (sys_switch_state_get( SYS_WGSEN ) == SYS_WGSEN_SLEEPING ))
            {
                res = pdPASS;
            }
        break;
        case SYS_WAKEUP_TRIG_NET:   /* 6 NET中断事件唤醒 */
            if((sys_switch_state_get( SYS_WNET ) == SYS_WNET_INIT ) || ( sys_switch_state_get( SYS_WNET ) == SYS_WNET_SLEEPING ))
            {
                res = pdPASS;
            }
        break;
        case SYS_WAKEUP_TRIG_TIME:   /* 7 TIME中断事件唤醒 */
            //if(sys_switch_state_get( SYS_WTIME ) == SYS_WTIME_INIT )
            //{
                res = pdPASS;
            //}
        break;
    }
    return res;
}

/** ****************************************************************************
* @remarks       uint32_t system_wakeup_queue_set( uint32_t msg )
* @brief         设置系统唤醒消息队列信息
* @param[in]      *msg 数据源地址
* @param[out]    无
* @return        pdPASS 成功      pdFAIL 失败
* @attention     无
*******************************************************************************/
uint32_t system_wakeup_queue_set( uint32_t msg )
{
    event_trig_msg_s trig_msg;
    uint32_t ret = pdFAIL;
    system_work_mode_s sys_work_st = SYS_ST_INIT;
    sys_work_st = sys_work_mode_get();
    if( sys_work_st == SYS_ST_SLEEP )
    {
        sys_wakeup_sem_reg = msg;
        gaoz_sem_signal( sys_wakeup_semaphore );
        ret = pdPASS;
    }
    else if( sys_work_st == SYS_ST_STOP )
    {
        sys_wakeup_sem_reg = msg;
        gaoz_sem_signal( sys_wakeup_semaphore );
        ret = pdPASS;
    }
    else if( sys_work_st == SYS_ST_WAKE )
    {
        if( system_wakeup_signal_detection( msg ) == pdPASS )
        {
            trig_msg.command_id = EVENT_TRIG_INT;
            trig_msg.returns_res = msg;
            event_queue_put_isr( (event_trig_msg_s *)&trig_msg );
            ret = pdPASS;
        }
    }
    return ret;
}

/** ****************************************************************************
* @remarks       void system_wakeup_event_queue_set( uint32_t msg )
* @brief         设置系统唤醒消息队列信息
* @param[in]      *msg 数据源地址
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void system_wakeup_event_queue_set( uint32_t msg )
{
    event_trig_msg_s trig_msg;
    trig_msg.command_id = EVENT_TRIG_INT;
    trig_msg.returns_res = msg;
    event_queue_put_isr( (event_trig_msg_s *)&trig_msg );
}

/** ****************************************************************************
* @remarks       static void system_into_sleep_work(void)
* @brief         进入睡眠之前的工作
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_into_sleep_work(void)
{
    event_trig_msg_s trig_msg;
    uint32_t wakeup_msg = 0;
    gaoz_set_sleep_mode( 3 );                             /*进入系统休眠*/
    system_block_mark = SYS_SUSPEND;                      /*系统阻塞*/
    for( ;; )
    {
        if( system_wakeup_queue_get( &wakeup_msg ) == pdPASS )
        {   
            gaoz_log_debug_printf( INFOR_MODE,"800c","mangpatio auth exit sleep\n", 0); 
            gaoz_set_sleep_mode( 0 );                     /*退出系统休眠*/
            system_block_mark = SYS_RESUME;               /*系统释放*/
            mon_wakeup_task_dog();                        /*唤醒系统*/
            sys_work_mode_set( SYS_ST_WAKE );             /*系统被唤醒*/
            trig_msg.command_id = EVENT_TRIG_INT;
            trig_msg.returns_res = wakeup_msg;
            event_queue_put( (event_trig_msg_s *)&trig_msg );
            break;
        }
        else
        {
            system_wakeup_rtc_time_get();
        }
    }
}
/** ****************************************************************************
* @remarks       static void system_into_reset_work(void)
* @brief         进入系统复位之前的工作
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_into_reset_work(void)
{ 
    static uint8_t net_err_restart_time = 0;

     gaoz_log_debug_printf( INFOR_MODE,"800c","<<<<<<<<<<*****SYSTEM goes into RESET mode*****>>>>>>>>>> [%d]\n",  sys_abnormal_reset_get()); 
    // log_debug_send(INFOR_MODE,MON_TASK_NAME,"<<<<<<<<<<*****SYSTEM goes into RESET mode*****>>>>>>>>>>\r\n", 0);
    system_net_led_state_control( LED_CLOSE );
    system_gnss_led_state_control( LED_CLOSE );
    gnss_info_flash_write();
    car_save_data_to_flash();
    log_error_code_flash_write();
    gaoz_task_sleep( 1000 );
    system_block_mark = SYS_SUSPEND;
    if(sys_abnormal_reset_get() != SYS_RESET_NET)//非网络导致的重启
    {
         gaoz_log_debug_printf( INFOR_MODE,"800c","<<<<<<<<<<*****SYSTEM goes into RESET mode*****>>>>>>>>>> !net [%d]\n",  sys_abnormal_reset_get()); 
        if( mcu_protocol_control_run_status( CONTROL_RESTART_CPU_ST ) != pdPASS )
        {
            gaoz_soft_reset();
        }
    }
    else//网络重启 仅重启模块 不重启MCU
    {
         gaoz_log_debug_printf( INFOR_MODE,"800c","<<<<<<<<<<*****SYSTEM goes into RESET mode*****>>>>>>>>>> Net [%d][%d]\n",  sys_abnormal_reset_get(),net_err_restart_time); 
        
         net_err_restart_time++;
        if(net_err_restart_time < 3)
        {
            if( mcu_protocol_control_run_status( CONTROL_ONLY_RESTART_CPU_ST ) != pdPASS )
            {
                gaoz_log_debug_printf( INFOR_MODE,"800c","<<<<<<<<<<*****SYSTEM goes into RESET mode*****>>>>>>>>>> Net only mcu restart!!!\n",  0); 
         
                gaoz_soft_reset(); 
            } 
        }
        else // 三次未重启成功 则全部重启CPU+MCU
        {
            if( mcu_protocol_control_run_status( CONTROL_RESTART_CPU_ST ) != pdPASS )
            {
                 gaoz_log_debug_printf( INFOR_MODE,"800c","<<<<<<<<<<*****SYSTEM goes into RESET mode*****>>>>>>>>>> Net only mcu + cpu restart!!!\n",  0); 
                gaoz_soft_reset();
            }
        }
       
    } 
    gaoz_task_sleep( 1000 );
 
}

/** ****************************************************************************
* @remarks       static void system_check_run_status(void)
* @brief         检测系统运行状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_check_run_status(void)
{
    system_work_mode_s sys_st = sys_work_mode_get();
    if(( sys_st == SYS_ST_START ) || ( sys_st == SYS_ST_RUN ))
    {
        if( sys_hard_state_get( SYS_GPRS ) == SYS_GPRS_NET_OK )     /* 检测NET的状态 ，网络状态正常*/
        {                                                           /*判断平台注册、登录的状态*/
            if(( sys_state_state_get( SYS_REG ) == SYS_REG_YES ) || ( sys_state_state_get( SYS_LAND ) ==  SYS_LADN_YES ))
            {
                system_net_led_state_control( LED_NORMAL );  
            }
            else
            {
                system_net_led_state_control( LED_ABNORMAL );
            }
        }
        else
        {
            system_net_led_state_control( LED_ABNORMAL );
        }
		if( sys_hard_state_get( SYS_GPS ) == SYS_GPS_POS_OK )    /* 检测GNSS的状态 ，定位状态正常*/
        {
            system_gnss_led_state_control( LED_NORMAL );
        }
        else
        {
            system_gnss_led_state_control( LED_ABNORMAL );
        }
        system_wakeup_rtc_time_stop();
    }
    else if(( sys_st == SYS_ST_SLEEP ) || ( sys_st == SYS_ST_STOP ))
    {
        system_net_led_state_control( LED_CLOSE );
        system_gnss_led_state_control( LED_CLOSE );
        /*系统检测到异常时进行立即复位*/
        if( sys_abnormal_reset_get() != SYS_RESET_INIT )
        {
            system_into_reset_work();
        }
        else
        {
            system_software_timer_stop();          /*关闭系统软件定时器*/
            system_wakeup_rtc_time_start();        /*启动系统唤醒定时器*/
            flash_spi_dev_close();
            gaoz_log_debug_printf( INFOR_MODE,"800c","mangpatio auth enter sleep\n", 0); 
            system_into_sleep_work();              /*进入系统休眠*/
            flash_spi_dev_open();
            system_software_timer_start();
        }
    }
    else if( sys_st == SYS_ST_WAKE )        
    {
        system_net_led_state_control( LED_WANKEUP );
        /*获取系统的唤醒时间*/
        if( system_wakeup_rtc_time_get() == pdPASS )
        {
            system_wakeup_rtc_time_start();
        }
    }
    /*系统检测SYS_RESET_NET 、SYS_RESET_MEM、SYS_RESET_TASK异常时进行立即复位*/
    if(( sys_abnormal_reset_get() != SYS_RESET_INIT ) && ( sys_abnormal_reset_get() != SYS_RESET_RUN ))
    {
        system_into_reset_work();
    }
}

/** ****************************************************************************
* @remarks       uint32_t mon_reg_task_dog( uint32_t  time_out , uint8_t * name ,task_dog_sem_e sem )
* @brief         注册喂狗事件
* @param[in]     TimeOutS 喂狗时间 单位s
* @param[in]     *name 注册任务的名称
* @param[out]    无
* @return        pdPASS 注册成功 ，pdFAIL注册失败
* @attention     无
*******************************************************************************/
uint32_t mon_reg_task_dog( uint32_t  time_out , uint8_t *name ,task_dog_sem_e sem )
{
    volatile uint32_t i;
    volatile uint16_t res = pdFAIL;
    uint32_t osR = 0;
    uint32_t task_id = 0;
    if(time_out < 60 * GAOZ_TICK_PERIOD_S  )
    {
        time_out = 60 * GAOZ_TICK_PERIOD_S;         //至少1分钟
    }
    if(time_out > 6000 * GAOZ_TICK_PERIOD_S )
    {
        time_out = 6000 * GAOZ_TICK_PERIOD_S ;      //不能大于100分钟
    }
    if( name == NULL )
    {
        return(res);
    }
    if(task_dog_count >= MAX_TASK_NUM)
    {
        return(res);
    }
    if( mon_system_mutex != 0 )
    {
        gaoz_mutex_lock(mon_system_mutex);
        osR = 1;
    }
    
    task_id = gaoz_get_thread_id();

    for(i = 0; i < MAX_TASK_NUM; i++) //先判断是否已经存在
    {
        if(task_dog_t[i].task_id == task_id)
        {
            task_dog_t[i].task_time_out  = time_out;
            task_dog_t[i].task_last_time = gaoz_get_systick();
            res = pdPASS;
        }
    }
    if( res != pdPASS )
    {
        for(i = 0; i < MAX_TASK_NUM; i++)   //查找空间
        {
            if(task_dog_t[i].task_id == 0)
            {
                task_dog_t[i].task_id = task_id;
                task_dog_t[i].task_time_out  = time_out;
                task_dog_t[i].task_last_time = gaoz_get_systick();
                task_dog_t[i].task_name = name;
                if( sem == TASK_SEM_YES )   //检测
                {
                    task_dog_t[i].task_semaphore = gaoz_sem_new(0);
                }
                task_dog_count++;
                res = pdPASS;
                break;
            }
        }
    }
    if(osR != 0)
    {
        gaoz_mutex_unlock(mon_system_mutex);
    }
    return(res);
}
/** ****************************************************************************
* @remarks       uint32_t mon_remove_task_dog(void)
* @brief         删除监控看门狗
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 删除失败 pdPASS 删除成功
* @attention     必须在任务中使用
*******************************************************************************/
uint32_t mon_remove_task_dog(void)
{
    volatile  uint32_t  res = pdFAIL;
    volatile  uint32_t  i;
    uint32_t  osR = 0;
    uint32_t task_id = 0;
    
    if( task_dog_count == 0 )
    {
        return(res);
    }
    if( mon_system_mutex != 0 )
    {
        gaoz_mutex_lock(mon_system_mutex);
        osR = 1;
    }
    task_id = gaoz_get_thread_id();
    for(i = 0; i < MAX_TASK_NUM; i++)
    {
        if(task_dog_t[i].task_id == task_id)
        {
            task_dog_t[i].task_id = 0;
            task_dog_t[i].task_time_out  = 0;
            task_dog_t[i].task_last_time = 0;
            task_dog_t[i].task_name = NULL;
            if( task_dog_t[i].task_semaphore != 0 )
            {
                gaoz_sem_free( task_dog_t[i].task_semaphore   );
            }
            task_dog_count--;
            res = pdPASS;
            break;
        }
    }

    if(osR != 0)
    {
        gaoz_mutex_unlock(mon_system_mutex);
    }
    return(res);
}


/** ****************************************************************************
* @remarks       uint32_t mon_reset_task_dog( void )
* @brief         复位看门狗时间
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 复位失败 pdPASS 复位成功
* @attention     必须在任务中使用
*******************************************************************************/
uint32_t mon_reset_task_dog( void )
{
    volatile  uint16_t  res = pdFAIL;
    volatile  uint32_t  i;
    uint32_t  osR = 0;
    uint32_t  task_id = 0;
    task_id = gaoz_get_thread_id();
    
    if( mon_system_mutex != 0 )
    {
        gaoz_mutex_lock(mon_system_mutex);
        osR = 1;
    }
    
    for(i = 0; i < MAX_TASK_NUM; i++)
    {
        if(task_dog_t[i].task_id == task_id)
        {
            task_dog_t[i].task_last_time = gaoz_get_systick();
            res = pdPASS;
            break;
        }
    }
    
    if(osR != 0)
    {
        gaoz_mutex_unlock(mon_system_mutex);
    }
    
    /*任务锁，检测系统休眠状态当系统处于休眠时处于长期休眠状态状态*/
    if(( task_dog_t[i].task_semaphore != 0 ) && ( system_block_mark == SYS_SUSPEND ) )
    {
        gaoz_sem_wait( task_dog_t[i].task_semaphore );
    }
    
    return(res);
}

/** ****************************************************************************
* @remarks       uint32_t mon_wakeup_task_dog( void )
* @brief         唤醒看门狗时间
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 复位失败 pdPASS 复位成功
* @attention     必须在任务中使用
*******************************************************************************/
static uint32_t mon_wakeup_task_dog( void )
{
    volatile  uint16_t  res = pdFAIL;
    volatile  uint32_t  i;
    uint32_t  osR = 0;
        
    if( mon_system_mutex != 0 )
    {
        gaoz_mutex_lock(mon_system_mutex);
        osR = 1;
    }
     gaoz_log_debug_printf( INFOR_MODE,"800c","mangpatio auth sleep -- all\n", 0);     
    for(i = 0; i < MAX_TASK_NUM; i++)
    {
        if(( task_dog_t[i].task_id != 0 ) && ( task_dog_t[i].task_semaphore != 0 ))
        {
            task_dog_t[i].task_last_time = gaoz_get_systick();      /*获取系统最新的时钟tick*/
            gaoz_sem_signal( task_dog_t[i].task_semaphore );        /*发送消息队列*/
            res = pdPASS;
        }
    }
    
    if(osR != 0)
    {
        gaoz_mutex_unlock(mon_system_mutex);
    }

    return(res);
}

/** ****************************************************************************
* @remarks       uint32_t system_wakeup_rtc_time_set( system_rtc_wakeup_t rtc,uint32_t wakeup_time )
* @brief         设置系统唤醒定时器时间
* @param[in]     wakeup_time 设置唤醒时间，单位S。时间小于等于3600S
* @param[out]    无
* @return        pdFAIL 设置失败 pdPASS 设置成功
* @attention     必须在任务中使用
*******************************************************************************/
uint32_t system_wakeup_rtc_time_set( system_rtc_wakeup_t rtc,uint32_t wakeup_time )
{
    if( wakeup_time > 3600 )
    {
        return pdFAIL;
    }
   
    if( rtc == RTC_NET )
    {
        //  gaoz_log_debug_printf( INFOR_MODE,"800c","mangpatio auth system_wakeup_rtc_time_set RTC_NET:%d\n", wakeup_time); 
        system_wakeup[0].counter = wakeup_time;
    }
    else if( rtc == RTC_VEH )
    {
        //  gaoz_log_debug_printf( INFOR_MODE,"800c","mangpatio auth system_wakeup_rtc_time_set RTC_VEH:%d\n", wakeup_time); 
        system_wakeup[1].counter = wakeup_time;
    }
    return pdPASS;
}

/** ****************************************************************************
* @remarks       void system_wakeup_rtc_time_start( void )
* @brief         获取系统唤醒定时器时间
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void system_wakeup_rtc_time_start( void )
{   
    #define SYS_WAKEUP_RTC_TIME ( 60 ) 
    
    if(( system_wakeup[0].counter != 0 ) && ( system_wakeup[0].timer == WAKEUP_INIT ))
    {
        system_wakeup[0].timer = WAKEUP_START;
        rtc_sys_get( RTC_TIM_TIMESTAMP, (uint32_t *)&system_wakeup[0].handle );
    }

    if(( system_wakeup[1].counter != 0 ) && ( system_wakeup[1].timer == WAKEUP_INIT ))
    {
        system_wakeup[1].timer = WAKEUP_START;
        rtc_sys_get( RTC_TIM_TIMESTAMP, (uint32_t *)&system_wakeup[1].handle );
    }
}

/** ****************************************************************************
* @remarks       void system_wakeup_rtc_time_stop( void )
* @brief         停止系统唤醒定时器时间
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void system_wakeup_rtc_time_stop( void )
{   
    if( system_wakeup[0].timer == WAKEUP_START  )
    {
        system_wakeup[0].timer = WAKEUP_INIT;
    }
    
    if( system_wakeup[1].timer == WAKEUP_START   )
    {
        system_wakeup[1].timer = WAKEUP_INIT;
    }
}
/** ****************************************************************************
* @remarks       uint32_t system_wakeup_rtc_time_get( void )
* @brief         获取系统唤醒定时时间
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     需要周期性调用，建议单位时间1S
*******************************************************************************/
uint32_t system_wakeup_rtc_time_get( void )
{
    uint32_t ret = pdFAIL;
    uint32_t wakeup_timer = 0;
    if( system_wakeup[0].timer == WAKEUP_START )
    {
        rtc_sys_get( RTC_TIM_TIMESTAMP, (uint32_t *)&wakeup_timer );
        if( wakeup_timer > system_wakeup[0].handle )
        {
            if(( wakeup_timer - system_wakeup[0].handle ) >= system_wakeup[0].counter )
            {
                system_wakeup[0].timer = WAKEUP_INIT;
                ret = system_wakeup_queue_set( SYS_WAKEUP_TRIG_RTC );
            }
        }
    }
    if( system_wakeup[1].timer == WAKEUP_START )
    {
        rtc_sys_get( RTC_TIM_TIMESTAMP, (uint32_t *)&wakeup_timer );
        if( wakeup_timer > system_wakeup[1].handle )
        {
            if(( wakeup_timer - system_wakeup[1].handle ) >= system_wakeup[1].counter )
            {
                system_wakeup[1].timer = WAKEUP_INIT;
                ret = system_wakeup_queue_set( SYS_WAKEUP_TRIG_TIME );
            }
        }
    }
    return ret;
}

/** ****************************************************************************
* @remarks       static void system_check_spaces_num(void)
* @brief         检测动态分配空间的状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_check_spaces_num(void)
{
    static uint32_t sys_space_time = 0;
    uint32_t spaces_num = 0;
    spaces_num = gaoz_malloc_mem_size();
    if( spaces_num > 10 )
    {   
        log_debug_printf(ERROR_MODE,MON_TASK_NAME ,"[%s]System Amount of memory allocated out of safe range:%d\r\n", sys_err_infor_pointer_get( 3 ),spaces_num );          
        if( ( ++sys_space_time ) > 120 )                       /*连续2分钟内没有释放内存，认为系统异常启动系统复位*/
        {    
            sys_space_time = 120;
            if(( sys_abnormal_reset_get() == SYS_RESET_INIT ) || ( sys_abnormal_reset_get() == SYS_RESET_RUN ))
            {
                reset_reason_set(EVENT_RESET_REASON_MEMROY_LEAK);
                sys_abnormal_reset_set( SYS_RESET_MEM );        /*设置系统长时间运行复位操作*/
            }
        }
    }
    else if( spaces_num > 0 )
    {
        sys_space_time = 0;
        log_debug_printf(INFOR_MODE,MON_TASK_NAME ,"System dynamically allocates the amount of memory:%d\r\n", spaces_num );
    }
    else
    {
        sys_space_time = 0;
    }
}
/** ****************************************************************************
* @remarks       static void system_run_time_detection(void)
* @brief         系统长时间复位机制
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     检测系统运行的时间，如果连续运行7日后重新启动系统(暂时保留此功能)
*******************************************************************************/
static void system_run_time_detection(void)
{
    if( system_timer_counter_reg > SYS_RST_TIME_MS )        /*连续运行7天，系统在休眠是进行复位*/
    {
        if( sys_abnormal_reset_get() == SYS_RESET_INIT )
        {   
            reset_reason_set(EVENT_RESET_REASON_RUN_7_DAY);
            sys_abnormal_reset_set( SYS_RESET_RUN );        /*设置系统长时间运行复位操作*/
            log_debug_printf(ERROR_MODE,MON_TASK_NAME ,"[%s]System runs continuously for 7*24 hours",sys_err_infor_pointer_get( 4 ));
        }
    }
}

/** ****************************************************************************
* @remarks       static void system_task_dog_manage(void)
* @brief         系统任务看门狗管理
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     检测注册系统看门狗的任务，是否超出设置时间。如果超出系统进行复位。
*******************************************************************************/
static void system_task_dog_manage(void) 
{
    #define SYS_TICK_VALUE ( 0xFFFFFFFF ) /*最大值*/
    volatile uint32_t i = 0 ,ret = pdFAIL;
    static uint32_t his_tick = 0; /*历史系统TICK时钟*/
    static uint32_t cur_tick = 0; /*当前系统TICK时钟*/
    //static uint32_t sys_dog_time = 0; 
    
    cur_tick = gaoz_get_systick();
    for( i = 0; i < MAX_TASK_NUM; i++ )
    {
        if(task_dog_t[i].task_time_out == 0)
        {
            continue;
        }
        if( cur_tick >= task_dog_t[i].task_last_time )                     /*系统TICK时间正常计时*/
        {
            if(( cur_tick - task_dog_t[i].task_last_time ) >= task_dog_t[i].task_time_out )
            {
                task_dog_t[i].task_last_time = cur_tick;                   /*更新系统时间戳*/
                his_tick = SYS_TICK_VALUE - task_dog_t[i].task_last_time;  /*剩余记录时间*/
                ret = pdPASS;
                break;
            }
        }
        else  /*系统TICK溢出*/
        {     /*当前记录的系统时钟加上溢出之前系统时钟 大于等于 设置的延时时间 */
            if(( cur_tick + his_tick ) >= task_dog_t[i].task_time_out )
            {
                task_dog_t[i].task_last_time= cur_tick;                     /*更新系统时间戳*/
                his_tick = SYS_TICK_VALUE - task_dog_t[i].task_last_time;   /*剩余记录时间*/
                ret = pdPASS;
                break;
            }
        }
    }
    
    if( ret == pdPASS )
    {
        log_debug_printf(ERROR_MODE,MON_TASK_NAME ,"[%s][%s]Task running abnormal system needs to be reset\r\n", sys_err_infor_pointer_get( 1 ), task_dog_t[i].task_name );
        //sys_dog_time++;
        //if( sys_dog_time > 5 )
        //{
        //    sys_dog_time = 5;
            if(( sys_abnormal_reset_get() == SYS_RESET_INIT ) || ( sys_abnormal_reset_get() == SYS_RESET_RUN ))
            {
                reset_reason_set(EVENT_RESET_REASON_SOFTWAR_DOG);
                sys_abnormal_reset_set( SYS_RESET_TASK );        /*设置系统长时间运行复位操作*/
                system_into_reset_work();                        /*防止CPU资源被强制占用，快速复位*/
            }
        //}
    }
    //else
    //{
    //    sys_dog_time = 0;
    //}
}

/** ****************************************************************************
* @remarks       static void system_timer_anomaly_detection(void)
* @brief         系统定时器异常检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     主要是用来检测系统定时器是否出现异常，如果出现异常。
*******************************************************************************/
static void system_timer_anomaly_detection(void) 
{
    static uint8_t mon_timer = 0;
    static uint32_t timer_tick = 0;
    
    if( sys_hard_state_get( SYS_TIMER ) == SYS_TIMER_FAULE )
    {
        system_timer_counter_reg++; /*当系统定时器运行异常时，进行定时器寄存器加一操作*/
    }
    else
    {
        if( timer_tick == gaoz_get_systick() )
        {
            mon_timer++;
            if( mon_timer > 3 ) 
            {
                mon_timer = 0;
                sys_hard_state_set( SYS_TIMER , SYS_TIMER_FAULE );
                if( sys_abnormal_reset_get() == SYS_RESET_INIT )
                {
                    reset_reason_set(EVENT_RESET_REASON_RTC_ERR);
                    sys_abnormal_reset_set( SYS_RESET_RUN );    /*系统定时器异常，等待系统进入休眠时复位系统*/
                    log_debug_printf(ERROR_MODE,MON_TASK_NAME ,"[%s]System timer is running abnormally\r\n", sys_err_infor_pointer_get( 2 ));
                }
            }
        }
        else
        {
            sys_hard_state_set( SYS_TIMER , SYS_TIMER_NORMAL );
            timer_tick = gaoz_get_systick();
            mon_timer = 0;
        }
    }

}

/** ****************************************************************************
* @remarks       static void system_free_space_monitoring(void)
* @brief         检测系统运行内存
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void system_free_space_monitoring(void)
{
    static uint8_t sys_mon_time = 0;
    uint32_t size,avail_size,max_block_size;
    if( ( ++sys_mon_time ) >= 3 ) /*3秒钟周期间隔检测系统栈空间尺寸*/
    {
        sys_mon_time = 0;
        gaoz_get_heapinfo( &size , &avail_size , &max_block_size );
        log_debug_printf(INFOR_MODE,MON_TASK_NAME ,"System stack space size:%d,%d,%d(tsize,asize,msize)\r\n",size , avail_size , max_block_size );
    }
}
/** ****************************************************************************
* @remarks       static uint8_t system_check_run_time( void )
* @brief         系统监控运行时间计数器
* @param[in]     无
* @param[out]    无
* @return        1 计数器溢出，0 计数器未溢出
* @attention     系统监控任务周期为100ms，其他时间检测需要1S钟进行检测一次。
*******************************************************************************/
static uint8_t system_check_run_time( void )
{
    mon_tick_counter ++;
    if( mon_tick_counter >= 10 )
    { 
        mon_tick_counter = 0;
        return 1;
    }
    return 0;
}
/** ****************************************************************************
* @remarks       uint8_t sys_hard_state_get(system_hardware_e hard_id )
* @brief         获取系统的硬件状态
* @param[in]     hara_id 获取硬件状态的ID 
* @param[out]    无
* @return        data_out 输出针对状态的数据
* @attention     无
*******************************************************************************/
uint8_t sys_hard_state_get(system_hardware_e hard_id )
{
    uint8_t data_out;
    switch( hard_id )
    {
        case SYS_POWER:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_power_s;
        break;
        case SYS_VEHICLE:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.veh_power_s;
        break;
        case SYS_BAT:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_bat_s;
        break;
        case SYS_RTC:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_rtc_s;
        break;
        case SYS_GPS:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_gps_s;
        break;
        case SYS_GPRS:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_gprs_s;
        break;
        case SYS_GSEN:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_gsen_s;
        break;
        case SYS_CAN:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_can_s;
        break;
        case SYS_FLASH:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_flash_s;
        break;
        case SYS_BLE:
           data_out = ( uint8_t )sys_run_s.sys_hard_s.system_ble_s; 
        break; 
        case SYS_SIM:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.system_sim_s; 
        break;   
        case SYS_TIMER:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_timer_s; 
            break;

        case SYS_VEHICLE_ACC:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_vehicle_acc_s;
        break;   

        case SYS_VEHICLE_READY:
            data_out = ( uint8_t )sys_run_s.sys_hard_s.sys_vehicle_ready_s;
        break;   

        default:
            data_out = 0xFF;
            break;
    }
    return data_out;
}
/** ****************************************************************************
* @remarks       void sys_hard_state_set(system_hardware_e hara_id ,uint8_t data_in)
* @brief         设置系统的硬件状态
* @param[in]     hara_id 获取硬件状态的ID 
* @param[in]     data_in 设置的指定状态的数据
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void sys_hard_state_set(system_hardware_e hara_id ,uint8_t data_in)
{
    switch( hara_id )
    {
        case SYS_POWER:
            sys_run_s.sys_hard_s.sys_power_s = ( system_power_e )data_in;
        break;
        case SYS_VEHICLE:
            sys_run_s.sys_hard_s.veh_power_s = ( vehicle_power_e )data_in;
        break;
        case SYS_BAT:
            sys_run_s.sys_hard_s.sys_bat_s = ( system_battery_e )data_in;
        break;
        case SYS_RTC:
            sys_run_s.sys_hard_s.sys_rtc_s = ( system_rtc_e )data_in;
        break;
        case SYS_GPS:
            sys_run_s.sys_hard_s.sys_gps_s = ( system_gps_e )data_in;
        break;
        case SYS_GPRS:
            sys_run_s.sys_hard_s.sys_gprs_s = ( system_gprs_e )data_in;
        break;
        case SYS_GSEN:
            sys_run_s.sys_hard_s.sys_gsen_s = ( system_Gsen_e )data_in;
        break;
        case SYS_CAN:
            sys_run_s.sys_hard_s.sys_can_s = ( system_can_e )data_in;
        break;
        case SYS_FLASH:
            sys_run_s.sys_hard_s.sys_flash_s = ( system_flash_e )data_in;
        break;
        case SYS_BLE:
            sys_run_s.sys_hard_s.system_ble_s = ( system_ble_e )data_in;
        break; 
        case SYS_SIM:
            sys_run_s.sys_hard_s.system_sim_s = ( system_sim_e )data_in; 
        break;    
        case SYS_TIMER:
            sys_run_s.sys_hard_s.sys_timer_s = ( system_timer_e )data_in;
        break;

        case SYS_VEHICLE_ACC:
            sys_run_s.sys_hard_s.sys_vehicle_acc_s = (system_vehicle_warning_e)data_in;
            break;

        case SYS_VEHICLE_READY:
            sys_run_s.sys_hard_s.sys_vehicle_ready_s = (system_vehicle_warning_e)data_in;
            break;

        default:
            break;
    }
}
/** ****************************************************************************
* @remarks       uint8_t sys_switch_state_get(system_witch_e switch_id )
* @brief         获取硬件的开关状态
* @param[in]     switch_id 获取硬件状态的ID 
* @param[out]    无
* @return        返回指定状态的数据
* @attention     无
*******************************************************************************/
uint8_t sys_switch_state_get(system_witch_e switch_id )
{   
    uint8_t data_out;
    switch( switch_id )
    {
        case SYS_KL15:
            data_out = ( uint8_t )sys_run_s.sys_switch_s.sys_kl15_s;
        break;
        case SYS_WVEH:
            data_out = ( uint8_t )sys_run_s.sys_switch_s.sys_wveh_s;
        break;   
        case SYS_WCAN:
            data_out = ( uint8_t )sys_run_s.sys_switch_s.sys_wcan_s;
        break;
        case SYS_WRTC:
            data_out = ( uint8_t )sys_run_s.sys_switch_s.sys_wrtc_s;
        break;
        case SYS_WNET:
            data_out = ( uint8_t )sys_run_s.sys_switch_s.sys_wnet_s;
        break;   
        case SYS_WBLE:
            data_out = ( uint8_t )sys_run_s.sys_switch_s.sys_wble_s;
        break;    
        case SYS_WGSEN:
            data_out = ( uint8_t )sys_run_s.sys_switch_s.sys_wgsen_s;
        break;    
        case SYS_WTIME:
            data_out = ( uint8_t )sys_run_s.sys_switch_s.sys_wtime_s;
        break;     
        default:
            data_out = 0xFF;
            break;
    }
    return data_out;
}



/** ****************************************************************************
* @remarks       void sys_switch_state_set(system_witch_e switch_id ,uint8_t data_in)
* @brief         设置系统开关状态
* @param[in]     switch_id 设置系统开关状态的ID 
* @param[in]     data_in 设置设置系统开关状态
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void sys_switch_state_set(system_witch_e switch_id ,uint8_t data_in)
{
    switch( switch_id )
    {
        case SYS_KL15:
            sys_run_s.sys_switch_s.sys_kl15_s = ( system_kl15_e )data_in;
        break;
        case SYS_WVEH:
            sys_run_s.sys_switch_s.sys_wveh_s = ( system_wveh_e )data_in;
        break;
        case SYS_WCAN:
            sys_run_s.sys_switch_s.sys_wcan_s = ( system_wcan_e )data_in;
        break;
        case SYS_WRTC:
            sys_run_s.sys_switch_s.sys_wrtc_s = ( system_wrtc_e )data_in;
        break;
        case SYS_WNET:
            sys_run_s.sys_switch_s.sys_wnet_s = ( system_wnet_e )data_in;
        break;   
        case SYS_WBLE:
            sys_run_s.sys_switch_s.sys_wble_s = ( system_wble_e )data_in;
        break;    
        case SYS_WGSEN:
            sys_run_s.sys_switch_s.sys_wgsen_s = ( system_wgsen_e )data_in;
        break;   
        case SYS_WTIME:
            sys_run_s.sys_switch_s.sys_wtime_s = ( system_wtime_e )data_in;
        break;     
        default:
            break;
    }
}
/** ****************************************************************************
* @remarks       uint8_t sys_state_state_get(system_state_e state_id)
* @brief         获取系统状态
* @param[in]     state_id 获取系统状态的ID 
* @param[out]    无
* @return        获取指定ID系统状态的值
* @attention     无
*******************************************************************************/
uint8_t sys_state_state_get(system_state_e state_id)
{
    uint8_t data_out;
    switch( state_id )
    {
        case SYS_RUN:
            data_out = ( uint8_t )sys_run_s.sys_state_s.sys_run_s;
        break;
        case SYS_REG:
            data_out = ( uint8_t )sys_run_s.sys_state_s.sys_reg_s;
        break;
        case SYS_LAND:
            data_out = ( uint8_t )sys_run_s.sys_state_s.sys_land_s;
        break;
        case SYS_OTA:
            data_out = ( uint8_t )sys_run_s.sys_state_s.sys_ota_s;
        break;
        case SYS_POS:
            data_out = ( uint8_t )sys_run_s.sys_state_s.sys_pos_s;
        break;
        default:
            data_out = 0xFF;
            break;
    }
    return data_out;
}
/** ****************************************************************************
* @remarks       void sys_state_state_set(system_state_e state_id ,uint8_t data_in)
* @brief         设置系统状态
* @param[in]     state_id 设置系统状态的ID
* @param[in]     data_in 设置系统状态
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void sys_state_state_set(system_state_e state_id ,uint8_t data_in)
{
    switch( state_id )
    {
        case SYS_RUN:
            sys_run_s.sys_state_s.sys_run_s = (system_run_e)data_in;
        break;
        case SYS_REG:
            sys_run_s.sys_state_s.sys_reg_s = (system_reg_e)data_in;
        break;
        case SYS_LAND:
            sys_run_s.sys_state_s.sys_land_s = (system_land_e)data_in;
        break;
        case SYS_OTA:
            sys_run_s.sys_state_s.sys_ota_s = (system_ota_e)data_in;
        break;
        case SYS_POS:
            sys_run_s.sys_state_s.sys_pos_s = (system_pos_mode_e)data_in;
        break;
        default:
            break;
    }
}
/** ****************************************************************************
* @remarks       system_work_mode_s sys_work_mode_get(void)
* @brief         获取系统总状态
* @param[in]     无
* @param[out]    无
* @return        系统当前的状态
* @attention     无
*******************************************************************************/
system_work_mode_s sys_work_mode_get(void)
{
    return sys_run_s.sys_work_s;
}

/** ****************************************************************************
* @remarks       void sys_work_mode_set(system_work_mode_s data_in)
* @brief         设置系统总状态
* @param[in]     data_in 设置系统总状态的参数
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void sys_work_mode_set(system_work_mode_s data_in)
{
    sys_run_s.sys_work_s = data_in;
}

/** ****************************************************************************
* @remarks       system_abnormal_reset_e sys_abnormal_reset_get(void)
* @brief         获取系统异常复位标志位
* @param[in]     无
* @param[out]    无
* @return        系统当前的状态
* @attention     无
*******************************************************************************/
system_abnormal_reset_e sys_abnormal_reset_get(void)
{
    return system_abnormal_reset;
}

/** ****************************************************************************
* @remarks       void sys_abnormal_reset_set( system_abnormal_reset_e data_in )
* @brief         设置系统异常复位标志位
* @param[in]     data_in 设置参数
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void sys_abnormal_reset_set( system_abnormal_reset_e data_in )
{
    system_abnormal_reset = data_in;
}

/** ****************************************************************************
* @remarks       uint32_t sys_timestamp_delay( uint32_t* plast_timestamp, uint32_t delay )
* @brief         时间戳延时 ，单位S
* @param[in]     delay 延时时间
* @param[out]    plast_timestamp:上次到期时间戳指针
* @return        pdPASS 成功/ pdFAIL失败
* @attention     时间戳到期后自动更新上次时间戳到新时间戳
*******************************************************************************/
uint32_t sys_timestamp_delay( uint32_t* plast_timestamp, uint32_t delay )
{
    uint32_t cur_tick = 0;              /*当前系统TICK时钟*/
    
    cur_tick = gaoz_get_systick();
    if(( cur_tick - *plast_timestamp ) >= delay )
    {
        *plast_timestamp = cur_tick;    /*更新系统时间戳*/
        return pdPASS;
    }
    
    return pdFAIL;
}

/** ****************************************************************************
* @remarks       static void mon_system_task( void * pvParameters)
* @brief         系统监控任务
* @param[in]     * pvParameters 任务参数，未使用
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void mon_system_task( void * pvParameters)
{
    (void) pvParameters;
    gaoz_task_sleep( 100 );         /*等待系统任务创建完成*/ 
    mon_reg_task_dog( 60 * GAOZ_TICK_PERIOD_S ,(uint8_t * )MON_TASK_NAME , TASK_SEM_NO );
    log_debug_send(DEBUG_MODE,MON_TASK_NAME ,"MON management task started successfully\r\n",0);
    for(;;)
    {
        mon_reset_task_dog();
        gaoz_watchdog_feed();       /*系统喂狗操作*/
        if( system_check_run_time() == 1 )
        {
            /*检测系统剩余空间*/
            system_free_space_monitoring();
            /*任务看门狗周期性检测*/
            system_task_dog_manage();
            /*动态申请内存空间检测*/
            system_check_spaces_num();
            /*系统长时间运行休眠机制*/
            system_run_time_detection();
            /*检测系统定时器运行状态检测*/
            system_timer_anomaly_detection();
        }
        /*系统休眠机制的判断*/
        system_check_run_status();
        gaoz_task_sleep( 100 );     /*系统延时100ms钟的时间*/
    }
}

